package com.lanou3g.baidumusic.main.musicfragment.listfragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.baidumusic.R;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/17.
 */

public class ListFragmentAdapter extends RecyclerView.Adapter<ListFragmentAdapter.ListHolder>{


    private Context context ;
    private ListBean listBean;
    private ListHolder holder;


    public void setListBean(ListBean bean){
        this.listBean = bean ;
        notifyDataSetChanged();
    }

    public ListFragmentAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_fragment_list , parent , false);

        holder = new ListHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ListHolder holder, int position) {


        holder.titleTv.setText(listBean.getContent().get(position).getName());
        holder.oneTv.setText(listBean.getContent().get(position).getContent().get(0).getTitle()
                + " - " + listBean.getContent().get(position).getContent().get(0).getAuthor());
        holder.twoTv.setText(listBean.getContent().get(position).getContent().get(1).getTitle()
                + " - " + listBean.getContent().get(position).getContent().get(1).getAuthor());
        holder.threeTv.setText(listBean.getContent().get(position).getContent().get(2).getTitle()
                + " - " + listBean.getContent().get(position).getContent().get(2).getAuthor());

        Picasso.with(context).load(listBean.getContent().get(position).getPic_s192()).into(holder.picIv);
        if(position == getItemCount()-1){
            holder.blankTv.setVisibility(View.VISIBLE);
            Log.d("ListFragmentAdapter", "blankTv");
        }



    }

    @Override
    public int getItemCount() {

        if (null == listBean){
            return 0 ;
        }
        if (null == listBean.getContent()){
            return 0 ;
        }

        return listBean.getContent() != null && listBean.getContent().size() > 0 ? listBean.getContent().size() : 0;
    }



    public class ListHolder extends RecyclerView.ViewHolder{

        TextView titleTv ,oneTv , twoTv, threeTv,blankTv;
        ImageView picIv ;


        public ListHolder(View itemView) {
            super(itemView);

            titleTv = (TextView) itemView.findViewById(R.id.item_fragment_list_title_tv);
            oneTv = (TextView) itemView.findViewById(R.id.item_fragment_list_one_tv);
            twoTv = (TextView) itemView.findViewById(R.id.item_fragment_list_two_tv);
            threeTv = (TextView) itemView.findViewById(R.id.item_fragment_list_three_tv);
            blankTv = (TextView) itemView.findViewById(R.id.item_fragment_blank_tv);

            picIv = (ImageView) itemView.findViewById(R.id.item_fragment_list_iv);

        }
    }


}
